package com.hushui.mall.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.mall.commodity.entity.vo.AttrGroupWithAttrVo;
import com.hushui.mall.commodity.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.commodity.entity.AttrGroupEntity;
import com.hushui.mall.commodity.service.AttrGroupService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * 属性分组
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:46
 */
@RestController
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    /**
     * 列表
     */
    @RequestMapping("commodity/attrgroup/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    //3-
    @GetMapping("/product/attrgroup/list/{catelogId}")
    public R QueryAttrGroup(@PathVariable(name = "catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", pageUtils);
    }
    //4-
    @GetMapping("/product/attrgroup/info/{attrGroupId}")
    public R QueryAttrGroup(@PathVariable(name = "attrGroupId") Long attrGroupId){
        QueryWrapper<AttrGroupEntity> wrapper= new QueryWrapper<>();
        wrapper.eq("attr_group_id",attrGroupId);
        AttrGroupEntity one = attrGroupService.getOne(wrapper);
        return R.ok().put("attrGroup", one);
    }
    /**
     * 信息
     */
    @RequestMapping("commodity/attrgroup/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }
    @RequestMapping("/product/attrgroup/{attrgroupId}/noattr/relation")
    public R getAttr(@PathVariable(value = "attrgroupId")Long attrgroupId,@RequestParam Map<String, Object> params){


        return R.ok();
    }
    @RequestMapping("/product/attrgroup/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable(value = "attrgroupId")Long catelogId){
        List<AttrGroupWithAttrVo> list= attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",list);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/attrgroup/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/attrgroup/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("commodity/attrgroup/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
